using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class CarDAL
    {
        public static int Save(CarInfo info)
        {
            string sql = string.Format("insert into [car]([AdminID],[UserID],[ProductID],[OPrice],[APrice],[PPrice],[Unit],[Count],[IsCheck],[Format],[CreateDate]) values(@AdminID,@UserID,@ProductID,@OPrice,@APrice,@PPrice,@Unit,@Count,@IsCheck,@Format,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@UserID", info.UserID),
                new SqlParameter("@ProductID", info.ProductID),
                new SqlParameter("@OPrice", info.OPrice),
                new SqlParameter("@APrice", info.APrice),
                new SqlParameter("@PPrice", info.PPrice),
                new SqlParameter("@Unit", info.Unit),
                new SqlParameter("@Count", info.Count),
                new SqlParameter("@IsCheck", info.IsCheck),
                new SqlParameter("@Format", info.Format),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(CarInfo info)
        {
            string sql = string.Format("update [car] set [AdminID]=@AdminID,[ProductID]=@ProductID,[Count]=@Count,[IsCheck]=@IsCheck,[Format]=@Format where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@ProductID", info.ProductID),
                new SqlParameter("@Count", info.Count),
                new SqlParameter("@IsCheck", info.IsCheck),
                new SqlParameter("@Format", info.Format),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static int Update(int id, int count)
        {
            return CommonDAL.ExecuteNoneQuery(string.Format("update [car] set Count={0} where ID={1};", count, id)) ? id : 0;
        }
        public static bool Set_IsCheck(int adminid, int userid, string ids)
        {
            if (ids == "") { ids = "0"; }
            return CommonDAL.ExecuteNoneQuery(string.Format("update [car] set IsCheck=0 where AdminID={0} and UserID={1};update [car] set IsCheck=1 where AdminID={0} and ID in ({2});", adminid, userid, ids));
        }
        
        public static CarInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[car]");
            return SimpleData(dr);
        }
        public static List<CarInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[car]");
            return ListSimpleData(dt);
        }
        public static List<CarInfo> FindPage(string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [car].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [car].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[car]", out count);
            return ListSimpleData(dt);
        }
        public static List<CarInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [car]"));
            return ListSimpleData(dt);
        }
        public static List<CarInfo> FindListByUserID(int adminid, int userid)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select [car].*,[product].Name,[product].Imgs,[product].ExpressMoney from [car] left join [product] on [car].ProductID=[product].ID where [car].AdminID={0} and [car].UserID={1} order by [car].ID desc", adminid, userid));
            return ListAllData(dt);
        }
        public static List<CarInfo> FindListByPay(int adminid, int userid)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select [car].*,[product].Name,[product].Imgs,[product].ExpressMoney from [car] left join [product] on [car].ProductID=[product].ID where [car].AdminID={0} and [car].UserID={1} and [car].IsCheck=1 order by [car].ID desc", adminid, userid));
            return ListAllData(dt);
        }
        public static bool DeleteListByPay(int adminid, int userid)
        {
            return CommonDAL.ExecuteNoneQuery(string.Format("delete from [car] where AdminID={0} and UserID={1} and IsCheck=1", adminid, userid));
        }
        public static CarInfo FindIDByUserIDAndProductID(int adminid, int userid, int productid, string format)
        {
            DataRow dr = CommonDAL.GetDataRow(string.Format("select * from [car] where AdminID={0} and UserID={1} and ProductID={2} and Format='{3}'", adminid, userid, productid, format));
            return SimpleData(dr); 
        }
        private static CarInfo SimpleData(DataRow dr)
        {
            CarInfo info = new CarInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.ProductID = DataConvert.ToInt(dr["ProductID"]);
                info.OPrice = DataConvert.ToFloat(dr["OPrice"]);
                info.APrice = DataConvert.ToFloat(dr["APrice"]);
                info.PPrice = DataConvert.ToFloat(dr["PPrice"]);
                info.Unit = DataConvert.ToString(dr["Unit"]);
                info.Count = DataConvert.ToInt(dr["Count"]);
                info.IsCheck = DataConvert.ToInt(dr["IsCheck"]);
                info.Format = DataConvert.ToString(dr["Format"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static CarInfo AllData(DataRow dr)
        {
            CarInfo info = new CarInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.ProductID = DataConvert.ToInt(dr["ProductID"]);
                info.OPrice = DataConvert.ToFloat(dr["OPrice"]);
                info.APrice = DataConvert.ToFloat(dr["APrice"]);
                info.PPrice = DataConvert.ToFloat(dr["PPrice"]);
                info.Unit = DataConvert.ToString(dr["Unit"]);
                info.Count = DataConvert.ToInt(dr["Count"]);
                info.IsCheck = DataConvert.ToInt(dr["IsCheck"]);
                info.Format = DataConvert.ToString(dr["Format"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);

                info.Name = DataConvert.ToString(dr["Name"]);
                info.Imgs = (Commons.domain + DataConvert.ToString(dr["Imgs"]).Replace(",", "," + Commons.domain)).Split(',');
                info.ExpressMoney = DataConvert.ToFloat(dr["ExpressMoney"]);
                info.Img = info.Imgs[0];
                info.OPriceText = info.OPrice.ToString("0.00");
                info.APriceText = info.APrice.ToString("0.00").Split('.');
            }
            return info;
        }
        private static List<CarInfo> ListSimpleData(DataTable dt)
        {
            List<CarInfo> list = new List<CarInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<CarInfo> ListAllData(DataTable dt)
        {
            List<CarInfo> list = new List<CarInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }
    }
}

